package com.example.admin.controller;

import com.example.admin.service.BlogService;
import com.example.xo.common.Result;
import com.example.xo.pojo.Blog;
import com.example.xo.vo.BlogVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author linzinan
 */
@Slf4j
@RestController
@RequestMapping("blog")
public class BlogController {
    @Autowired
    private BlogService blogService;

    @PostMapping()
    public Result add(@RequestBody Blog blog){
        if (blog.getTitle()==null || blog.getTitle().trim().length()==0){
            log.info("标题为空");
            return Result.error();
        }
        if (blog.getTitle().length()>50){
            log.info("标题过长");
            return Result.error();
        }
        if (blog.getCategoryId()==null||blog.getCategoryId().trim().length()==0){
            log.info("分类数据丢失");
            return Result.error();
        }
        if (blog.getAuthor()==null||blog.getAuthor().trim().length()==0){
            log.info("作者数据丢失");
            return Result.error();
        }
        if (blog.getAuthor().length()>10){
            log.info("作者名字过长");
            return Result.error();
        }
        return Result.success(blogService.add(blog));
    }


    @GetMapping("")
    public Result findAll(){
        List<BlogVo> list = blogService.findAll();
        return Result.success(list);
    }

    @GetMapping("{id}")
    public Result findById(@PathVariable String id){
        log.info("{}",id);
        return Result.success(blogService.findPoById(id));
    }


    @DeleteMapping("{id}")
    public Result delete(@PathVariable String id) {
        return Result.success(blogService.deleteById(id));
    }

    @PutMapping
    public Result update(@RequestBody Blog blog){
        return Result.success(blogService.update(blog));
    }
}
